package com.jhgsys.internal.workflow.service.impl;

import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.workflow.entity.WorkflowNodeTrans;
import com.jhgsys.internal.workflow.mapper.WorkflowNodeTransMapper;
import com.jhgsys.internal.workflow.service.IWorkflowNodeTransService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 流程节点流向表 Service实现
 *
 * @author fenghaijun
 * @date 2024-07-19 09:56:47
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class WorkflowNodeTransServiceImpl extends ServiceImpl<WorkflowNodeTransMapper, WorkflowNodeTrans> implements IWorkflowNodeTransService {

    /**
     * 查询（分页）
     * @author fenghaijun
     * @date 2024-07-19 09:56:47
     * @param request QueryRequest
     * @param workflowNodeTrans workflowNodeTrans
     * @return IPage<WorkflowNodeTrans>
    */
    @Override
    public IPage<WorkflowNodeTrans> findWorkflowNodeTranss(QueryRequest request, WorkflowNodeTrans workflowNodeTrans) {
        LambdaQueryWrapper<WorkflowNodeTrans> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowNodeTrans::getDeletedFlg, FebsConstant.VALID);
        // TODO 设置查询条件

        Page<WorkflowNodeTrans> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author fenghaijun
     * @date 2024-07-19 09:56:47
     * @param workflowNodeTrans workflowNodeTrans
     * @return List<WorkflowNodeTrans>
     */
    @Override
    public List<WorkflowNodeTrans> findWorkflowNodeTranss(WorkflowNodeTrans workflowNodeTrans) {
	    LambdaQueryWrapper<WorkflowNodeTrans> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowNodeTrans::getDeletedFlg, FebsConstant.VALID);
        if(null != workflowNodeTrans.getTenantId()){
            queryWrapper.eq(WorkflowNodeTrans::getTenantId, workflowNodeTrans.getTenantId());
        }
        if(null != workflowNodeTrans.getWorkflowId()){
            queryWrapper.eq(WorkflowNodeTrans::getWorkflowId, workflowNodeTrans.getWorkflowId());
        }
        if(null != workflowNodeTrans.getWorkflowNodeFromId()){
            queryWrapper.eq(WorkflowNodeTrans::getWorkflowNodeFromId, workflowNodeTrans.getWorkflowNodeFromId());
        }
        if(null != workflowNodeTrans.getWorkflowNodeToId()){
            queryWrapper.eq(WorkflowNodeTrans::getWorkflowNodeToId, workflowNodeTrans.getWorkflowNodeToId());
        }
        if(null != workflowNodeTrans.getWorkflowNodeTransFlg()){
            queryWrapper.eq(WorkflowNodeTrans::getWorkflowNodeTransFlg, workflowNodeTrans.getWorkflowNodeTransFlg());
        }
        if(null != workflowNodeTrans.getWorkflowNodeTransType()){
            queryWrapper.eq(WorkflowNodeTrans::getWorkflowNodeTransType, workflowNodeTrans.getWorkflowNodeTransType());
        }
        queryWrapper.orderByAsc(WorkflowNodeTrans::getWorkflowNodeTransOrder);
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author fenghaijun
    * @date 2024-07-19 09:56:47
    * @param id
    * @return WorkflowNodeTrans
    */
    @Override
    public WorkflowNodeTrans findWorkflowNodeTransById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author fenghaijun
     * @date 2024-07-19 09:56:47
     * @param workflowNodeTrans workflowNodeTrans
     */
    @Override
    @Transactional
    public void createWorkflowNodeTrans(WorkflowNodeTrans workflowNodeTrans) {
        workflowNodeTrans.setCreatedTime(new Date());
        this.save(workflowNodeTrans);
    }

    /**
     * 修改
     * @author fenghaijun
     * @date 2024-07-19 09:56:47
     * @param workflowNodeTrans workflowNodeTrans
     */
    @Override
    @Transactional
    public void updateWorkflowNodeTrans(WorkflowNodeTrans workflowNodeTrans) {
        workflowNodeTrans.setUpdatedTime(new Date());
        this.saveOrUpdate(workflowNodeTrans);
    }

    /**
     * 逻辑删除
     * @author fenghaijun
     * @date 2024-07-19 09:56:47
     * @param workflowNodeTrans workflowNodeTrans
     * @param workflowNodeTransIds workflowNodeTransIds
     */
    @Override
    @Transactional
    public void deleteWorkflowNodeTrans(WorkflowNodeTrans workflowNodeTrans,String workflowNodeTransIds) {
        List<String> list = Arrays.asList(workflowNodeTransIds.split(StringPool.COMMA));
        this.baseMapper.update(workflowNodeTrans,new LambdaQueryWrapper<WorkflowNodeTrans>().in(WorkflowNodeTrans::getWorkflowNodeTransId,list));
	}

    /**
    * 物理删除
    * @author fenghaijun
    * @date 2024-07-19 09:56:47
    * @param workflowNodeTransIds workflowNodeTransIds
    */
    @Override
    @Transactional
    public void physicallyDeleteWorkflowNodeTrans(String workflowNodeTransIds) {
        List<String> list = Arrays.asList(workflowNodeTransIds.split(StringPool.COMMA));
        LambdaQueryWrapper<WorkflowNodeTrans> wapper = new LambdaQueryWrapper<>();
        wapper.in(WorkflowNodeTrans::getWorkflowNodeTransId,list);
        this.remove(wapper);
    }
}
